export class MemoryManager
{
    private static _globalheap:Record<string,any>={}//Map<string,any> = new Map<string,any>()
    public static getMemory<T extends IMemoryCache=IMemoryCache>(region:string)
    {
        if(region == Game.shard.name)
        {
            // 全局的
            return this.getRegionMemory<T>(["Global"]);
        }
        else
        {
            // 
            return this.getRegionMemory<T>(["rooms",region]);
        }
    }

    private static initMemory(keys:string[])
    {
        let preMemory = Memory as any;
        for(const k of keys)
        {
            if(!preMemory[k])
            {
                preMemory[k]={};
                preMemory=   preMemory[k];
            }
        }
    }

    private static getRegionMemory<T extends IMemoryCache>(keys:string[]):T
    {
        let preMemory = Memory as any;
        for(const k of keys)
        {
            preMemory = preMemory[k];
        }
        return preMemory
    }
    public static allocHeap(guid:string)
    {
        if(this.getHeap(guid))
        {
            return this.getHeap(guid);
        }
        else 
        {
            this._globalheap[guid]={};
            // global.Log.Warning(`初始化堆:${guid}`)
            return this.getHeap(guid);
        }
        
    }
    public static getHeap(guid:string)
    {
        return this._globalheap[guid]
    }
    public static deleteNodeHeap(guid:string)
    {
        this._globalheap[guid]=undefined;
    }
}